package com.honour.controller.interfaced;

import java.io.InputStream;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.Const;
import com.system.util.FTPUtils;
import com.system.util.PageData;
import com.system.util.SpringApplicationUtil;
import com.system.util.Tools;
import com.honour.service.interfaced.impl.DiscoverInService;


public class MySubThread extends BaseController implements Runnable{
	
//	@Resource(name="discoverinService")
//	private DiscoverInService discoverinService;
	
    //private DiscoverInService discoverinService=new DiscoverInService();
	
	private  String TITLE;
	private String HONOURUSER_ID;
	private String DIS_TYPE;
	private String CAIDAN_ID;
	private MultipartHttpServletRequest mreq;
	private HttpServletResponse resp;
	private HttpServletRequest request;
	private InputStream is;
	private MultipartFile file;
	private InputStream stream;

	@Override
	public void run() {
		addSelfMediaAd(mreq,
                 resp, request,
                 TITLE, HONOURUSER_ID, DIS_TYPE, CAIDAN_ID, is,stream);
		
	}

	
	
	
	/**
	 * 异步上传视频和缩略图
	 * @Author zhaozhenhua
	 * @date   2018年1月13日
	 * @return
	 */
	@ResponseBody
	public Object addSelfMediaAd(MultipartHttpServletRequest mreq,
			                     HttpServletResponse resp,HttpServletRequest request,
			                     String TITLE,String HONOURUSER_ID,String DIS_TYPE,
			                     String CAIDAN_ID,InputStream is,InputStream stream 
			                     ){
		logBefore(logger, "异步上传视频和缩略图");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		//pd = this.getPageData();
		String result = "00";
		String SHOWVIDEO="";
		String VIDEOBACKIMAGE="";
		try{
				if(DIS_TYPE.equals("2001")){
					//if(AppUtil.checkParam("addParty", pd)){	//检查参数
					// 获取上传视频的路径
					String uploadFilePath = mreq.getFile("showfile").getOriginalFilename();
					// 截取上传视频的后缀
					String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1,
							uploadFilePath.length());
					// 用UUID来解决同名文件
					String upload = UUID.randomUUID().toString().replace("-", "");
					// 获取传输过来的Input流
					// 根据txt文件读取ftp工具类上传需要的参数
					String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
					String[] split = updeploy.split(",");
					// 第一个参数为 图片服务器的地址
					// 第二个参数为 图片服务器接口
					// 第三/四个参数为 FTP服务器的登录用户名和密码
					String path = "discover/video";
					FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path,
							upload + "." + uploadFileSuffix, is);
					// 通过工具类获取图片服务器地址
					String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
				    SHOWVIDEO = imgpath + path + "/" + upload + "." + uploadFileSuffix;
				    
				    
				   // 获取上传视频缩略图的路径
					String uploadImgPath = mreq.getFile("fileList").getOriginalFilename();
					// 截取上传视频缩略图的后缀
					String uploadImgSuffix = uploadImgPath.substring(uploadImgPath.indexOf('.') + 1,
							uploadImgPath.length());
					// 用UUID来解决同名文件
					String uploadImg = UUID.randomUUID().toString().replace("-", "");
					// 第一个参数为 图片服务器的地址
					// 第二个参数为 图片服务器接口
					// 第三/四个参数为 FTP服务器的登录用户名和密码
					String pathImg = "discover/videobackimg";
					FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], pathImg,
							uploadImg + "." + uploadImgSuffix, stream);
					 VIDEOBACKIMAGE = imgpath + pathImg + "/" + uploadImg + "." + uploadImgSuffix;
				    
				pd.put("DISCOVERVIDEO_ID", this.getSnowId());
				pd.put("VIDEOBACKIMAGE", VIDEOBACKIMAGE);
				//发布时间
				Date date = new Date();
				String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
				pd.put("ADDTIME", format);
				pd.put("TITLE", TITLE);
				pd.put("SHOWVIDEO", SHOWVIDEO);
				pd.put("ISSHOW", "1");  
				pd.put("CAIDAN_ID", CAIDAN_ID);  
				pd.put("DIS_TYPE", DIS_TYPE);    
				pd.put("HONOURUSER_ID", HONOURUSER_ID);
				DiscoverInService discoverinService = (DiscoverInService)SpringApplicationUtil.getSpringBean("discoverinService");
				discoverinService.addSelfMediaAd(pd);
				//返回01为查询成功并返回
				result =   "01";
				}else{
					result = "05";
				}	
			
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	public String getTITLE() {
		return TITLE;
	}

	public void setTITLE(String tITLE) {
		TITLE = tITLE;
	}

	public String getHONOURUSER_ID() {
		return HONOURUSER_ID;
	}

	public void setHONOURUSER_ID(String hONOURUSER_ID) {
		HONOURUSER_ID = hONOURUSER_ID;
	}

	public String getDIS_TYPE() {
		return DIS_TYPE;
	}

	public void setDIS_TYPE(String dIS_TYPE) {
		DIS_TYPE = dIS_TYPE;
	}

	public String getCAIDAN_ID() {
		return CAIDAN_ID;
	}

	public void setCAIDAN_ID(String cAIDAN_ID) {
		CAIDAN_ID = cAIDAN_ID;
	}

	public MultipartHttpServletRequest getMreq() {
		return mreq;
	}

	public void setMreq(MultipartHttpServletRequest mreq) {
		this.mreq = mreq;
	}

	public InputStream getIs() {
		return is;
	}

	public void setIs(InputStream is) {
		this.is = is;
	}

	public MultipartFile getFile() {
		return file;
	}

	public void setFile(MultipartFile file) {
		this.file = file;
	}
	
	public void setRequest(HttpServletRequest request) {
		this.request=request;
	}
	
	public void setResponse(HttpServletResponse response) {
		this.resp=response;
	}




	public InputStream getStream() {
		return stream;
	}




	public void setStream(InputStream stream) {
		this.stream = stream;
	}

}
